<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonModule for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace User;

use User\Model\UserInRoleTable;

use Zend\Db\ResultSet\HydratingResultSet;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Mvc\ModuleRouteListener;
use Zend\Authentication\Storage;
use Zend\Authentication\AuthenticationService;
use Zend\Authentication\Adapter\DbTable as DbTableAuthAdapter;

use User\Model\UserTable;
use User\Model\RoleTable;
use User\Model\Entity\User;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;


class Module implements AutoloaderProviderInterface
{
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
		    // if we're in a namespace deeper than one level we need to fix the \ in the path
                    __NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__),
                ),
            ),
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function onBootstrap($e)
    {
        // You may not need to do this if you're doing it elsewhere in your
        // application
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
        
        $sharedManager = $eventManager->getSharedManager();
        
        $sharedManager->attach('Zend\Mvc\Controller\AbstractActionController', 'dispatch', function($e){
        	
        	
	        if($e->getApplication()->getServiceManager()->get('AuthService')->hasIdentity())
	        {
	        	$users = $e->getApplication()->getServiceManager()->get('User\Model\TtkAuthStorage')->read();
	        }
	        else
	        {
// 	        	if($e->getRouteMatch()->getMatchedRouteName() != 'user/process' && $e->getRouteMatch()->getParam('controller') != "User\\Controller\\Auth")
// 	        	{
	        			 
// 	        		$url = $e->getRouter()->assemble(array('controller' => 'auth', 'action' => 'login'), array('name' => 'user/process'));
	        		
// 		            $response = $e->getResponse();
		            
// 		            $response->setStatusCode(302);
		            
// 		            $response->getHeaders()->addHeaderLine('Location', $url);
		            
// 		            $e->stopPropagation(true); 
		            
// 		            return $response;
	
// 	        	}
	        
        	}
        });
    }
    
    public function getServiceConfig()
    {
    	return array(
    			'factories' => array(
    					'User\Model\TtkAuthStorage' => function($sm){
    							return new \User\Model\TtkAuthStorage('user_auth');
    						},
    						
    					'AuthService' => function($sm) {
    							$dbAdapter = $sm->get('\Zend\Db\Adapter\Adapter');
    							$dbTableAuthAdapter = new DbTableAuthAdapter($dbAdapter, 'users', 'username', 'password');
    							$authService = new AuthenticationService();
    							$authService->setAdapter($dbTableAuthAdapter);
    							$authService->setStorage($sm->get('User\Model\TtkAuthStorage'));
    							
    							return $authService;
    						},
    						
    					
    					'User\Model\UserTable' => function($sm) {
    							$tableGateway = $sm->get('UserTableGateway');
    							$table = new UserTable($tableGateway);
    							return $table;
    						},
    						 
    							
    					'UserTableGateway' => function($sm) {
    							$adapter = $sm->get('Zend\Db\Adapter\Adapter');
    							$resultSetPrototype = new ResultSet();
    							$resultSetPrototype->setArrayObjectPrototype(new User());
    							return new TableGateway('users', $adapter, null, $resultSetPrototype);
    						},
    						
    					'User\Model\RoleTable' => function($sm)	{
		    					
    							$tableGateway = $sm->get('RoleTableGateway');
		    					$table = new RoleTable($tableGateway);
		    					return $table;
	    						
	    					},
	    					
	    				'RoleTableGateway' => function($sm) {
	    						
	    						$adapter = $sm->get('Zend\Db\Adapter\Adapter');
	    						
	    						return new TableGateway('role', $adapter, null, new HydratingResultSet());
	    						
	    					},
	    					
	    				'User\Model\UserInRoleTable' => function($sm)	{
	    					 
	    					$tableGateway = $sm->get('UserInRoleTableGateway');
	    					$table = new UserInRoleTable($tableGateway, $sm);
	    					return $table;
	    						
	    					},
	    					
	    				'UserInRoleTableGateway' => function($sm) {
	    						
	    					$adapter = $sm->get('Zend\Db\Adapter\Adapter');
	    						
	    					return new TableGateway('user_in_role', $adapter, null, new HydratingResultSet());
	    						
	    					},
	    					
	    				'User\Form\UserInRoleForm' => function($sm) {
	    					$roleTable = $sm->get('User\Model\RoleTable');
	    					$form =  new \User\Form\UserInRoleForm($roleTable);
	    					return $form;
	    					},
    						
    					),
    			
    			);
    					
    } 
}
